﻿using LiteDB;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace Cheems.Interface.Models.UserManagement
{
    public class T_User
    {
        public T_User()
        {
            CreatTime = DateTime.Now;
            UserLevel = UserLevel.None;
        }

        /// <summary>
        /// 用户名
        /// </summary>
        [SugarColumn(IsNullable = false, ColumnDescription = "用户名")]
        public string Name {  get; set; }

        /// <summary>
        /// 用户等级
        /// </summary>
        [SugarColumn(IsNullable = false, ColumnDescription = "用户等级")]
        public UserLevel UserLevel { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        [BsonIgnore]
        [SugarColumn(IsIgnore = true)]
        public string Password { get; set; }

        /// <summary>
        /// 密码Sha256Hash哈希值
        /// </summary>
        [SugarColumn(IsNullable = false, ColumnDescription = "密码Sha256Hash哈希值")]
        public string PasswordSha256Hash { get { return GetSha256Hash(Password); } }

        /// <summary>
        /// 用户创建时间
        /// </summary>
        [SugarColumn(IsNullable = false, ColumnDescription = "用户创建时间")]
        public DateTime CreatTime { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        [SugarColumn(IsNullable = true, ColumnDescription = "备注")]
        public string Remarks { get; set; }


        static string GetSha256Hash(string input)
        {
            using (SHA256 sha256Hash = SHA256.Create())
            {
                // 将字符串转换为字节数组并计算哈希值
                byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

                // 将字节数组转换为十六进制字符串
                StringBuilder builder = new StringBuilder();
                foreach (byte byteValue in bytes)
                {
                    builder.Append(byteValue.ToString("x2"));
                }
                return builder.ToString();
            }
        }
    }
}
